<?PHP
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/25
 * Time: 12:43
 */
namespace SDF\Lib\aliyun_sms;
require_once __DIR__.'/aliyun-php-sdk-core/Config.php';
use Sms\Request\V20160927 as Sms;
class aliyun_sms {
    private $template;
    private $signName;
    private $client_sms;
    public function __construct(){
        //todo 配置 as 和 ak
        $iClientProfile = \DefaultProfile::getProfile("cn-hangzhou", "9C0bWqgbtTfeDAa4", "ABerDHX0Rh4SaVDfIbOemCHlIXxEYX");
        $this->client_sms = new \DefaultAcsClient($iClientProfile);
    }

    /**
     * 设置模板
     * @param $code
     * @return $this
     */
    public function setTemplate($code){
        $this->template = $code;
        return $this;
    }

    /**
     * 设置签名
     * @param $name
     * @return $this
     */
    public function setSignName($name){
        $this->signName = $name;
        return $this;
    }
    public function send($number,$content){
        $request = new Sms\SingleSendSmsRequest();
        $request->setSignName($this->signName);
        $request->setTemplateCode($this->template);
        $request->setRecNum($number);
        $content = json_encode($content);
        $request->setParamString($content);
        try {
            $response = $this->client_sms->getAcsResponse($request);
            return array('status'=>true,'res'=>$response);
        }
        catch (\ClientException  $e) {
            $code = $e->getErrorCode();
            $msg = $e->getErrorMessage();
            return array('status'=>false,'res'=>$code.$msg);
        }
        catch (\ServerException  $e) {
            $code = $e->getErrorCode();
            $msg = $e->getErrorMessage();
            return array('status'=>false,'res'=>$code.$msg);
        }
    }
}
